package category

import (
	"fmt"
	"myblogs/app/variable"
)

type CategoryModel struct {
	Id       int64
	ParentId int64 `db:"parent_id"`
	Sort     string
	Name     string
	Cover    string
	Total    int64
}

// GetCategoryTotal 分类数量统计
func (c *CategoryModel) GetCategoryTotal() int64 {
	strSql := "select count(*) from category"
	var total int64
	err := variable.Db.Get(&total, strSql)
	if err != nil {
		fmt.Printf("%s", err)
		return 0
	}
	return total
}

// GetCategoryInfo 按分类ID查询分类信息
func (c *CategoryModel) GetCategoryInfo(id int64) CategoryModel {
	sqlStr := "select * from category where id=?"
	var category CategoryModel
	err := variable.Db.Get(&category, sqlStr, id)
	if err != nil {
		fmt.Printf("get failed, err:%v\n", err)
		return CategoryModel{}
	}
	return category
}

func (c *CategoryModel) GetCategoryAll() []*CategoryModel {
	strSql := "select * from category"
	var categorys []*CategoryModel
	err := variable.Db.Select(&categorys, strSql)
	if err != nil {
		return nil
	}
	return categorys
}
